package com.sgcc.lms.openstack.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sgcc.lms.openstack.exception.*;
import com.sgcc.lms.openstack.model.Parameter;
import com.sgcc.lms.openstack.model.po.Project;
import com.sgcc.lms.openstack.service.KeystoneService;
import com.sgcc.lms.openstack.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.identity.v3.User;
import org.openstack4j.openstack.identity.v3.domain.KeystoneUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-17 11:26
 * @Description: 认证与授权服务控制层
 */
@RestController
@Slf4j
@Api(tags = "KeyStoneController：认证服务")
public class KeyStoneController {

    @Autowired
    private KeystoneService keystoneService;

    private static final String LINE = "==========================================》{}";

    /**
     * @Author 李孟帅
     * @Description 用户认证
     * @Date 2020/7/18
     **/
    @PostMapping("/auth/token")
    @ApiOperation("用户认证")
    public ResultUtil<?> token(@RequestBody Parameter parameter) throws JsonProcessingException {
        //获取用户名
        String username = parameter.getUsername();
        //获取秘密
        String password = parameter.getPassword();
        //获取项目id
        String projectId = parameter.getProjectId();
        String projectName = parameter.getProjectName();
        String tokenId = null;
        //判断项目id是否存在，不存在则进行无范围的用户授权，否则进行项目范围的用户授权

        if (StringUtils.isEmpty(projectId)) {
            if (StringUtils.isEmpty(projectName)){
                //获取无范围授权的token
                tokenId = keystoneService.token(username, password);
                log.info("/auth/token,无范围授权,tokenId" + LINE, tokenId);
            }else {
                tokenId = keystoneService.tokenScope(username, password, projectName);
                log.info("/auth/token,项目范围授权,tokenId" + LINE, tokenId);
            }
        } else {
            //获取项目范围授权的token
            tokenId = keystoneService.token(username, password, projectId);
            log.info("/auth/token,项目范围授权,tokenId" + LINE, tokenId);
        }

        return ResultUtil.success(tokenId);
    }

    /**
     * @Author 李孟帅
     * @Description 获取当前用户可用的项目
     * @Date 2020/7/18
     **/
    @GetMapping("/auth/projects")
    @ApiOperation("获取当前用户可用的项目")
    public ResultUtil<?> authProjects(HttpServletRequest request) throws IOException, HttpException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        //获取可用的项目范围
        List<Project> projects = keystoneService.authProjects(tokenId);
        log.info(uri+",获取可用的项目范围"+LINE,"成功");
        return ResultUtil.success(projects);
    }

    /**
     * @Author 李孟帅
     * @Description 列出项目
     * @Date 2020/7/18
     **/
    @GetMapping("/projects")
    @ApiOperation("列出项目")
    public ResultUtil<?> projects(HttpServletRequest request) throws IOException, HttpException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        //列出项目
        List<Project> projects = keystoneService.projects(tokenId);
        log.info(uri+",列出项目"+LINE,"成功");
        return ResultUtil.success(projects);
    }

    /**
     * @Author 李孟帅
     * @Description 创建项目
     * @Date 2020/7/18
     **/
    @PostMapping("/projects/create")
    @ApiOperation("创建项目")
    public ResultUtil<?> createProject(HttpServletRequest request, @RequestBody Project project) throws HttpException, IOException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        //获取项目名
        String projectName = project.getName();
        //项目名不能为为null
        if (StringUtils.isEmpty(projectName)) {
            log.error("项目名" + LINE, "为空");
            return ResultUtil.result(0, "项目名不能为空");
        }
        Project result = keystoneService.createProject(tokenId, project);
        log.info(uri+",创建项目"+LINE,"成功");
        return ResultUtil.success(result);
    }

    /**
     * @Author 李孟帅
     * @Description 获取项目
     * @Date 2020/7/20
     **/
    @GetMapping("/projects/get")
    @ApiOperation("获取项目")
    public ResultUtil<?> getProject(HttpServletRequest request, @RequestParam(value = "id") String id) throws HttpException, IOException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Project project = keystoneService.getProject(tokenId, id);
        log.info(uri+",获取项目"+LINE,"成功");
        return ResultUtil.success(project);
    }

    /**
     * @Author 李孟帅
     * @Description 更新项目
     * @Date 2020/7/20
     **/
    @PostMapping("/projects/update")
    @ApiOperation("更新项目")
    public ResultUtil<?> updateProject(HttpServletRequest request, @RequestBody Project project) throws HttpException, IOException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Project result = keystoneService.updateProject(tokenId, project);
        log.info(uri+",更新项目"+LINE,"成功");
        return ResultUtil.success(result);
    }

    /**
     * @Author 李孟帅
     * @Description 删除项目
     * @Date 2020/7/20
     **/
    @GetMapping("/projects/delete")
    @ApiOperation("删除项目")
    public ResultUtil<?> deleteProject(HttpServletRequest request, @RequestParam(value = "id") String id) throws HttpException, IOException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        String result = keystoneService.deleteProject(tokenId, id);
        log.info(uri+",删除项目"+LINE,"成功");
        return ResultUtil.success(result);
    }

    /**
     * @Author 李孟帅
     * @Description 列出用户
     * @Date 2020/7/18
     **/
    @GetMapping("/users")
    @ApiOperation("列出用户")
    public ResultUtil<?> users(HttpServletRequest request) throws NullTokenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        //列出用户
        List<? extends User> users = keystoneService.users(tokenId);
        log.info(uri+",列出用户"+LINE,"成功");
        return ResultUtil.success(users);
    }

    /**
     * @Author 李孟帅
     * @Description 创建用户
     * @Date 2020/7/21
     **/
    @PostMapping("/users/create")
    @ApiOperation("创建用户")
    public ResultUtil<?> createUser(HttpServletRequest request, @RequestBody KeystoneUser user) throws NullTokenException, ForbiddenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        User user1 = keystoneService.createUser(tokenId, user);
        log.info(uri+",创建用户"+LINE,"成功");
        return ResultUtil.success(user1);
    }

    /**
     * @Author 李孟帅
     * @Description 获取用户
     * @Date 2020/7/21
     **/
    @GetMapping("/users/get")
    @ApiOperation("获取用户")
    public ResultUtil<?> getUser(HttpServletRequest request, @RequestParam(value = "id") String id) throws NullTokenException, ForbiddenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        User user = keystoneService.getUser(tokenId, id);
        if (Objects.isNull(user)) {
            return ResultUtil.result(0, "用户不存在");
        }
        log.info(uri+",获取用户"+LINE,"成功");
        return ResultUtil.success(user);
    }

    /**
     * @Author 李孟帅
     * @Description 更新用户
     * @Date 2020/7/21
     **/
    @PostMapping("/users/update")
    @ApiOperation("更新用户")
    public ResultUtil<?> updateUser(HttpServletRequest request, @RequestBody KeystoneUser user) throws NullTokenException, ForbiddenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        String id = user.getId();
        if (StringUtils.isEmpty(id)) {
            return ResultUtil.result(0, "用户id不允许为空");
        }
        User user1 = keystoneService.updateUser(tokenId, user);
        if (Objects.isNull(user1)) {
            return ResultUtil.result(0, "用户不存在");
        }
        log.info(uri+",更新用户"+LINE,"成功");
        return ResultUtil.success(user1);
    }

    /**
     * @Author 李孟帅
     * @Description 删除用户
     * @Date 2020/7/21
     **/
    @GetMapping("/users/delete")
    @ApiOperation("删除用户")
    public ResultUtil<?> deleteUser(HttpServletRequest request, @RequestParam(value = "id") String id) throws NullTokenException, ForbiddenException {
        //从请求头获取tokenId
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        ActionResponse actionResponse = keystoneService.deleteUser(tokenId, id);
        log.info(uri+",删除用户"+LINE,"成功");
        return ResultUtil.success(actionResponse);
    }
}
